## R Script Output -------------------------------------------------------------
# Appendix Figure D1: Pre-treatment Parallel Trends


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",
         "RColorBrewer", 
         "gridExtra")

lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/ISQ-frei-replication/"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "data-merge-county.RData", sep = ""))

# subset
df <- county.df


## Figure D1 -------------------------------------------------------------------
df.sum.bi <- df %>%
  group_by(cn_ug_sqmi_cat_2012_bi_high, year) %>%
  summarize(dem_share = mean(dem_share, na.rm = TRUE)) %>%
  spread(cn_ug_sqmi_cat_2012_bi_high, dem_share) %>%
  rename(`Low Exposure` = `0`,
         `High Exposure` = `1`) %>%
  gather(exposure_group, value, -year) %>%
  mutate(exposure_group = factor(exposure_group,
                                 levels = c("Low Exposure", 
                                            "High Exposure"))) %>%
  filter(year >= 2008 & year <= 2016)

# set parameters
linetype.high <- c("dotted", "solid")

axis.title.size <- 14

# plot
p.bi <- ggplot(df.sum.bi, 
               aes(x = year, 
                   y = value,
                   group = factor(exposure_group))) +
  geom_line(aes(linetype = as.factor(exposure_group))) + 
  scale_linetype_manual(values = linetype.high) +
  geom_vline(xintercept = 2012.5, 
             color = "red") +
  geom_point() +
  scale_x_continuous("",
                     breaks = seq(2008, 2016, by = 4), 
                     labels = seq(2008, 2016, by = 4),
                     limits = c(2008, 2016.5)) + 
  scale_y_continuous("Democratic Two-Party Vote Share",
                     breaks = seq(0.25, 0.55, by = 0.05),
                     labels = format(seq(0.25, 0.55, by = 0.05),
                                     nsmall = 1,
                                     scientific = FALSE),
                     limits = c(0.25, 0.55)) +
  theme_bw()  +
  theme(plot.title = element_text(size = axis.title.size + 2,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size - 1,
                                    margin = margin(0, 20, 0, 0)),
        axis.title.x = element_text(size = axis.title.size - 1,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 2),
        strip.text = element_text(size = axis.title.size - 1),
        strip.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        legend.position = "none") + 
  annotate("segment", 
           x = 2012.6, xend = 2014.5, 
           y = 0.55, yend = 0.55, 
           colour = "black",
           arrow = arrow(length = unit(0.2,"cm"), 
                         ends = "last", type = "closed")) +
  annotate("text", x = 2012.6, y = 0.51, label = "China's\nAnti-Corruption\nCampaign", hjust = 0) +
  annotate("text", x = 2009, y = 0.335, label = "Low 2012 CN-UG", hjust = 0) + 
  annotate("text", x = 2009, y = 0.47, label = "High 2012 CN-UG", hjust = 0) 

# save
pdf(file = paste(MAIN_DIR, "Figure-D1.pdf", sep = "/"),
    width = 5, height = 4.5)
print(p.bi)
dev.off()

